# @shuaige : 陈世玉
# @name :test4.py
# @time :2025/1/2 13:29
import requests
from lxml import etree

for i in range(398, 400):
    url = 'https://www.dongchedi.com/auto/params-carIds-x-' + str(i)
    headers = {
        'pragma': 'no-cache',
        'accept-language': 'zh-CN,zh;q=0.9',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36',
        'accept': '*/*',
        'cache-control': 'no-cache',
        'authority': 'www.dongchedi.com',
        'referer': 'https://www.dongchedi.com/auto/series/1234',
    }
    rs = requests.get(url=url, headers=headers)
    tree = etree.HTML(rs.text)

    try:
        # 使用 getall() 获取所有结果
        official_price = tree.xpath('//div[@data-row-anchor="official_price"]//div/text()') or [None]  # 官方价格
        sub_brand_name = tree.xpath('//div[@data-row-anchor="sub_brand_name"]//div/text()') or [None]  # 车商
        jb = tree.xpath('//div[@data-row-anchor="jb"]//div/text()') or [None]  # 级别
        fuel_form = tree.xpath('//div[@data-row-anchor="fuel_form"]//div/text()') or [None]  # 能源类型
        market_time = tree.xpath('//div[@data-row-anchor="market_time"]//div/text()') or [None]  # 上市时间
        engine_description = tree.xpath('//div[@data-row-anchor="engine_description"]//div/text()') or [None]  # 发动机
        energy_elect_max_power = tree.xpath('//div[@data-row-anchor="energy_elect_max_power"]//div/text()') or [None]  # 最大功率
        energy_elect_max_torque = tree.xpath('//div[@data-row-anchor="energy_elect_max_torque"]//div/text()') or [None]  # 最大扭矩
        gearbox_description = tree.xpath('//div[@data-row-anchor="gearbox_description"]//div/text()') or [None]  # 变速箱
        length_width_height = tree.xpath('//div[@data-row-anchor="length_width_height"]//div/text()') or [None]  # 长宽高
        car_body_struct = tree.xpath('//div[@data-row-anchor="car_body_struct"]//div/text()') or [None]  # 车身结构
        max_speed = tree.xpath('//div[@data-row-anchor="max_speed"]//div/text()') or [None]  # 最高时速
        acceleration_time = tree.xpath('//div[@data-row-anchor="acceleration_time"]//div/text()') or [None]  # 百公里加速时间
        wltc_fuel_comprehensive = tree.xpath('//div[@data-row-anchor="wltc_fuel_comprehensive"]//div/text()') or [None]  # WLTC综合油耗
        period = tree.xpath('//div[@data-row-anchor="period"]//div/text()') or [None]  # 保修期限
        maintain_cost = tree.xpath('//div[@data-row-anchor="maintain_cost"]//div/text()') or [None]  # 六万公里保养费用
    except Exception as e:
        print(f"Error: {e}")
        pass

    # 找到所有配置的最大长度，确保每个列表的长度一致
    max_length = max(len(official_price), len(sub_brand_name), len(jb), len(fuel_form), len(market_time),
                     len(engine_description), len(energy_elect_max_power), len(energy_elect_max_torque),
                     len(gearbox_description), len(length_width_height), len(car_body_struct), len(max_speed),
                     len(acceleration_time), len(wltc_fuel_comprehensive), len(period), len(maintain_cost))

    # 补充长度不足的列表
    official_price += [None] * (max_length - len(official_price))
    sub_brand_name += [None] * (max_length - len(sub_brand_name))
    jb += [None] * (max_length - len(jb))
    fuel_form += [None] * (max_length - len(fuel_form))
    market_time += [None] * (max_length - len(market_time))
    engine_description += [None] * (max_length - len(engine_description))
    energy_elect_max_power += [None] * (max_length - len(energy_elect_max_power))
    energy_elect_max_torque += [None] * (max_length - len(energy_elect_max_torque))
    gearbox_description += [None] * (max_length - len(gearbox_description))
    length_width_height += [None] * (max_length - len(length_width_height))
    car_body_struct += [None] * (max_length - len(car_body_struct))
    max_speed += [None] * (max_length - len(max_speed))
    acceleration_time += [None] * (max_length - len(acceleration_time))
    wltc_fuel_comprehensive += [None] * (max_length - len(wltc_fuel_comprehensive))
    period += [None] * (max_length - len(period))
    maintain_cost += [None] * (max_length - len(maintain_cost))

    # 打印数据
    for i in range(max_length):
        print(official_price[i], sub_brand_name[i], jb[i], fuel_form[i], market_time[i], engine_description[i],
              energy_elect_max_power[i], energy_elect_max_torque[i], gearbox_description[i], length_width_height[i],
              car_body_struct[i], max_speed[i], acceleration_time[i], wltc_fuel_comprehensive[i], period[i])
